//
//  ByBasketBallMixedCell.m
//  caipiao_ios
//
//  Created by mc on 2018/8/20.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBasketBallMixedCell.h"

@implementation ByBasketBallMixedCell

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByBasketBallMixedCell";
    ByBasketBallMixedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
        [self initViews];
    }
    return self;
}


- (void)initViews{
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.layer.cornerRadius = 10;
    backgroundView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(0);
        make.left.equalTo(self.contentView).offset(5);
        make.right.equalTo(self.contentView).offset(-5);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
    
    ByFootBallTitleView *titleView = [[ByFootBallTitleView alloc] init];
    [backgroundView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(backgroundView).offset(0);
        make.height.mas_equalTo(@85);
    }];
    self.titleView = titleView;
    
    CGFloat left = (SCREEN_WIDTH - 95 * 2 - 25 * 2 - 10 - 3) / 2;
    
    
    UILabel *pointLabel1 = [[UILabel alloc] init];
    [backgroundView addSubview:pointLabel1];
    [pointLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView.mas_bottom).offset(0);
        make.left.equalTo(backgroundView).offset(left);
        make.size.mas_equalTo(CGSizeMake(25, 36));
    }];
    pointLabel1.textColor = [UIColor whiteColor];
    pointLabel1.backgroundColor = rgba(116, 180, 226, 1);
    pointLabel1.text = @"非\n让\n分";
    pointLabel1.textAlignment = NSTextAlignmentCenter;
    pointLabel1.font = FONT(9);
    pointLabel1.numberOfLines = 0;
    
    UILabel *pointLabel2 = [[UILabel alloc] init];
    [backgroundView addSubview:pointLabel2];
    [pointLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pointLabel1.mas_bottom).offset(1);
        make.left.equalTo(pointLabel1.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(25, 36));
    }];
    pointLabel2.textColor = [UIColor whiteColor];
    pointLabel2.backgroundColor = BY_THEME_COLOR;
    pointLabel2.text = @"让\n分";
    pointLabel2.textAlignment = NSTextAlignmentCenter;
    pointLabel2.font = FONT(9);
    pointLabel2.numberOfLines = 0;
    self.pointLabel2 = pointLabel2;
    
    UIButton *morePlayTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundView addSubview:morePlayTypeBtn];
    [morePlayTypeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-left);
        make.top.equalTo(pointLabel1.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(25, 73));
    }];
    morePlayTypeBtn.backgroundColor = rgba(217, 184, 29, 1);
    [morePlayTypeBtn setTitle:@"更多\n玩法" forState:UIControlStateNormal];
    morePlayTypeBtn.titleLabel.font = FONT(11);
    [morePlayTypeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    morePlayTypeBtn.titleLabel.numberOfLines = 0;
    [morePlayTypeBtn addTarget:self action:@selector(morePlayAction:) forControlEvents:UIControlEventTouchUpInside];
    self.morePlayTypeBtn = morePlayTypeBtn;
    
    UIView *buttonsView = [[UIView alloc] init];
    [backgroundView addSubview:buttonsView];
    [buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pointLabel1.mas_right).offset(0);
        make.right.equalTo(morePlayTypeBtn.mas_left).offset(0);
        make.top.equalTo(pointLabel1.mas_top).offset(0);
        make.bottom.equalTo(morePlayTypeBtn.mas_bottom).offset(0);
    }];
    buttonsView.backgroundColor = BY_LINE_COLOR;
    self.buttonsView = buttonsView;
}


- (void)setDataArray:(NSMutableArray *)dataArray{
    
    _dataArray = dataArray;
    
    ByNetModel *model = [_dataArray firstObject];
    ByNetModel *secondModel = _dataArray[2];
    self.titleView.isBasketBall = YES;
    self.titleView.netModel = model;
    self.titleView.danguanView.hidden = model.single.integerValue == 0;
    [self.buttonsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (secondModel.fixedodds.integerValue < 0) {
        self.pointLabel2.backgroundColor = rgba(191, 225, 95, 1);
    }
    self.pointLabel2.text = secondModel.fixedodds;
    NSLog(@"1111111     %@",secondModel.fixedodds);
    CGFloat magin = 1;
    CGFloat width = 95;
    CGFloat height = 35;
    
    for (int i = 0;i < _dataArray.count; i++) {
        ByNetModel *netModel = [_dataArray objectAtIndex:i];
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(1 + i % 2 * (width + magin),1 + i / 2 * (height + magin), width,height)];
        [self.buttonsView addSubview:valueLabel];
        
        valueLabel.tag = 100 + i;
        valueLabel.font = FONT(14);
        valueLabel.textAlignment = NSTextAlignmentCenter;
        if (!netModel.selected) {
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
        }else{
            valueLabel.textColor = [UIColor whiteColor];
            valueLabel.backgroundColor = BY_THEME_COLOR;
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [valueLabel addGestureRecognizer:tap];
        
        
        if (netModel.isNoSale) {
            valueLabel.text = @"未开售";
            valueLabel.userInteractionEnabled = NO;
        }else{
            valueLabel.text = [NSString stringWithFormat:@"%@ %@", netModel.title,netModel.valueString];
            valueLabel.userInteractionEnabled = YES;
        }
    }
}

- (void)morePlayAction:(UIButton *)btn{
    if (self.morePlayBlock){
        self.morePlayBlock();
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    if (self.indexAtView) {
        self.indexAtView(tap.view.tag - 100);
    }
}

@end
